﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebFrontEnd.Helpers;

namespace WebFrontEnd
{
    public partial class QRCodes : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int eid = int.Parse(Request.QueryString["eid"]);
            BusinessEntities.Model.Event ev = BL.GetEventById(eid);
            if (ev.ID == GetCurrentUser().ID)
            {
                LoadInviteGifs(ev);
            }
            Response.Headers["content-disposition"] = "attachment; filename=" + ev.Name.Trim().Replace(' ', '_') + "_QRCodes.html";
        }

        private void LoadInviteGifs(BusinessEntities.Model.Event ev)
        {
            var invites = BL.GetInvitesByEvent(ev);
            int num = 0;
            StringBuilder html = new StringBuilder("<table><tr>");
            string domainName = ConfigurationManager.AppSettings["DomainName"];
            foreach (var invite in invites)
            {
                if (num % 3 == 0)
                {
                    html.Append("</tr><tr>");
                }
                string imgFormat = @"<img width=""64"" height=""64"" src=""http:/" + domainName + @"/QRGenerator.ashx?iid={0}""/>";
                html.Append(string.Format("<td>{0}<div>{1}</div></td>",invite.Guest.CombinedName(),string.Format(imgFormat,invite.ID)));
                num++;
            }
            html.Append("</tr></table>");
            mainTable.InnerHtml = html.ToString();
        }
    }
}